package com.zc.manager.device.domain;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import com.zc.supports.protocol.management.ProtocolSupportDefinition;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * 协议对象 iot_protocol
 * 
 * @author xiepufeng
 * @date 2022-08-30
 */
@Getter
@Setter
@ToString
public class IotProtocol extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 已发布状态 */
    public static final int DEPLOYED = 1;
    /** 未发布状态 */
    public static final int UNDEPLOY = 0;

    /** 协议ID */
    @Excel(name = "协议ID")
    @NotBlank(message = "协议ID不能为空")
    @Size(max = 50, message = "协议ID长度不能超过50个字符")
    private String id;

    /** 配置项 */
    @NotBlank(message = "其他配置不能为空")
    @Size(max = 1000, message = "其他配置长度不能超过1000个字符")
    private String configuration;

    /** 协议名称 */
    @Excel(name = "协议名称")
    @NotBlank(message = "协议名称不能为空")
    @Size(max = 30, message = "协议名称长度不能超过30个字符")
    private String protocolName;

    /** 协议类型 */
    @Excel(name = "协议类型")
    @Pattern(regexp = "jar|script", message = "无效的协议类型")
    private String protocolType;

    /** 发布状态 */
    @Excel(name = "发布状态")
    @Range(min = 0, max = 1, message = "无效的发布状态")
    private Integer publishState;

    /** 描述 */
    @Excel(name = "描述")
    @Size(max = 200, message = "描述不能超过200个字符")
    private String description;

    public ProtocolSupportDefinition toUnDeployDefinition() {
        ProtocolSupportDefinition definition = toDeployDefinition();
        definition.setPublishState((byte) UNDEPLOY);
        return definition;
    }

    public ProtocolSupportDefinition toDeployDefinition() {
        ProtocolSupportDefinition definition = new ProtocolSupportDefinition();
        definition.setId(getId());
        definition.setConfiguration(JSONObject.parseObject(configuration));
        definition.setName(protocolName);
        definition.setProvider(protocolType);
        definition.setPublishState((byte) DEPLOYED);

        return definition;
    }
}
